package GUI;
import 实体.会员卡;
import 操作.会员卡_操作;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

public class 售卡管理 {
    private JFrame frame;
    private JButton 返回会员登录Button;
    private JButton 系统管理Button;
    private JButton 会员信息Button;
    private JButton 消费记录Button;
    private JPanel main;
    private JTextField textField1;
    private JTextField textField2;
    private JComboBox comboBox1;
    private JTextField textField3;
    private JTextField textField4;
    private JButton 注册会员Button;

    public 售卡管理() {
        会员卡_操作 会员卡=new 会员卡_操作();

        comboBox1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String a="储蓄卡";
                String b="折扣卡";
                String a1=comboBox1.getSelectedItem().toString();
                if (a1.equals(a)){
                    textField3.setEnabled(true);
                    textField4.setEnabled(false);
                }
                if (a1.equals(b)){
                    textField4.setEnabled(true);
                    textField3.setEnabled(false);
                }
            }
        });
        注册会员Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    List<会员卡> list=null;
                    list=会员卡.查询all();
                    String[] s=new String[list.size()];
                    int count=0;
                    for (int i=0;i<list.size();i++){
                        s[i]=list.get(i).get卡号();
                        if (s[i].equals(textField2.getText())){
                            count=i;
                        }
                    }
                    if (textField2.getText().equals("")){
                        JOptionPane.showMessageDialog(null, "卡号输入不能为空！", "警告",JOptionPane.WARNING_MESSAGE);
                    }
                    else if(s[count].equals(textField2.getText())){
                        JOptionPane.showMessageDialog(null, "卡号已存在！", "警告",JOptionPane.WARNING_MESSAGE);
                    }
                    else {
                        String[] input=new String[6];
                        input[1] = textField1.getText();
                        input[0] = textField2.getText();
                        input[2] =comboBox1.getSelectedItem().toString();
                        String a="储蓄卡";
                        String b="折扣卡";
                        if (input[2].equals(a)){
                            input[3]=textField3.getText();
                            input[4]="0";
                        }
                        else if (input[2].equals(b)){
                            input[3]="0";
                            input[4]=textField4.getText();
                        }
                        input[5]="正常";
                        JOptionPane.showMessageDialog(null, "注册成功！");
                        会员卡.增加(input);
                        new 会员登录界面().界面();
                        frame.dispose();
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        });
        返回会员登录Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                new 会员登录界面().界面();
                frame.dispose();
            }
        });
    }
    public static void main(String[] args) {
        界面();
    }
    public static void 界面(){
        售卡管理 售卡=new 售卡管理();
        售卡.frame=new JFrame("游泳管理系统");
        售卡.frame.setContentPane(售卡.main);
        售卡.frame.setSize(500,400);
        售卡.frame.setLocation(500, 200);
        售卡.frame.setVisible(true);
        售卡.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
